<?php

namespace app\admin\controller;

use think\Db;


class Coupon extends Base
{

    public function getCouponList()
    {
        $res = Db::name('coupon')->order('status desc')->paginate($this->pageSize, false, ['page' => $this->pageId]);
        $res ? $this->suc($res) : $this->err('获取优惠券列表失败！');
    }

    public function addCoupon()
    {
        $data = [];
        if (input('couponType') == null) {
            $this->err('缺少couponType');
        } else {
            $data['couponType'] = input('couponType');
        }

        if (input('couponSubType') == null) {
            $this->err('缺少couponSubType');
        } else {
            $data['couponSubType'] = input('couponSubType');
        }

        if (input('name') == null) {
            $this->err('缺少name');
        } else {
            $data['name'] = input('name');
        }

        if (input('dateType') == null) {
            $this->err('缺少dateType');
        } else {
            $data['dateType'] = input('dateType');
        }

        if($data['dateType'] == 0){
            input('dateLimit') == null ? $this->err('缺少dateLimit') : $data['dateLimit'] = input('dateLimit');
        }

        if($data['dateType'] == 1){
            input('startTime') == null ? $this->err('缺少startTime') : $data['startTime'] = input('startTime');
            input('endTime') == null ? $this->err('缺少endTime') : $data['endTime'] = input('endTime');
        }

        if (input('total') == null) {
            $this->err('缺少total');
        } else {
            $data['total'] = input('total');
        }

        if (input('perLimit') == null) {
            $this->err('缺少perLimit');
        } else {
            $data['perLimit'] = input('perLimit');
        }

        if (input('status') == null) {
            $this->err('缺少status');
        } else {
            $data['status'] = input('status');
        }

        if (input('pickCount') == null) {
            $this->err('缺少pickCount');
        } else {
            $data['pickCount'] = input('pickCount');
        }
        if (input('fullAmount') == null) {
            $this->err('缺少fullAmount');
        } else {
            $data['fullAmount'] = input('fullAmount');
        }
        if (input('reduceAmount') == null) {
            $this->err('缺少reduceAmount');
        } else {
            $data['reduceAmount'] = input('reduceAmount');
        }
        $data['updateTime'] = createTime();
        $data['createTime'] = createTime();
        $res = Db::name('coupon')->insert($data);
        if ($res == 0) {
            $this->err('新增失败');
        } else if ($res == 1) {
            $this->suc('新增成功');
        }

    }

    public function editCoupon()
    {
        $data = [];
        if(input('id')==null){
            $this->err('缺少id');
        }else{
            $data['id'] =input('id');
        }

        if (input('couponType') == null) {
            $this->err('缺少couponType');
        } else {
            $data['couponType'] = input('couponType');
        }

        if (input('couponSubType') == null) {
            $this->err('缺少couponSubType');
        } else {
            $data['couponSubType'] = input('couponSubType');
        }

        if (input('name') == null) {
            $this->err('缺少name');
        } else {
            $data['name'] = input('name');
        }

        if (input('dateType') == null) {
            $this->err('缺少dateType');
        } else {
            $data['dateType'] = input('dateType');
        }

        if($data['dateType'] == 0){
            input('dateLimit') == null ? $this->err('缺少dateLimit') : $data['dateLimit'] = input('dateLimit');
        }

        if($data['dateType'] == 1){
            input('startTime') == null ? $this->err('缺少startTime') : $data['startTime'] = input('startTime');
            input('endTime') == null ? $this->err('缺少endTime') : $data['endTime'] = input('endTime');
        }

        if (input('total') == null) {
            $this->err('缺少total');
        } else {
            $data['total'] = input('total');
        }

        if (input('perLimit') == null) {
            $this->err('缺少perLimit');
        } else {
            $data['perLimit'] = input('perLimit');
        }

        if (input('status') == null) {
            $this->err('缺少status');
        } else {
            $data['status'] = input('status');
        }

        if (input('pickCount') == null) {
            $this->err('缺少pickCount');
        } else {
            $data['pickCount'] = input('pickCount');
        }

        if (input('fullAmount') == null) {
            $this->err('缺少fullAmount');
        } else {
            $data['fullAmount'] = input('fullAmount');
        }

        if (input('reduceAmount') == null) {
            $this->err('缺少reduceAmount');
        } else {
            $data['reduceAmount'] = input('reduceAmount');
        }

        $data['updateTime'] = createTime();
        $data['createTime'] = createTime();
        $res = Db::name('coupon')->update($data);
        if ($res == 0) {
            $this->err('数据未修改过,修改失败');
        } else if ($res == 1) {
            $this->suc('修改成功');
        }
    }

    /**
     * 删除用户优惠券
     */
    public function delUserCoupon(){
        $where = [];
        input('id') ? $where['id'] = input('id') : $this->err('缺少会员优惠券主键！');
        $res = model('UserCoupon')->where($where)->delete();
        $res ? $this->suc($res) : $this->err('删除失败！');

    }

    public function getCouponDetail()
    {
        $where = [];
        if (input('id') == null) {
            $this->err('缺少id');
        }else{
            $where['id'] = input('id');
        }
        $res = Db::name('coupon')->where($where)->find();
        $res ? $this->suc($res):$this->err('获取优惠券详情失败');
    }

    public function getUserCouponList(){
        if (input('cid') == null) {
            $this->err('缺少cid');
        }
        $res = Db::name('user_coupon')
            ->alias('uc')
            ->leftJoin('coupon c', 'uc.couponId = c.id')
            ->field('c.*,uc.currentNum,uc.pickNum')
            ->where([['uc.cid', '=', input('cid')], ['c.status', '=', 1]])
            ->paginate($this->pageSize, false, ['page' => $this->pageId]);
        $res ? $this->suc($res) : $this->err('获取优惠券列表失败');
    }

    /**
     * 更改优惠券状态
     */
    public function editCouponStatus(){
        $where = [];
        input('couponId') ? $where['id'] = input('couponId') : $this->err('缺少优惠券主键！');
        input('status') != null ? $where['status'] = input('status') : $this->err('缺少状态！');

        $coupon = model('coupon')->where(['id' => $where['id']])->find();
        if(!$coupon){
            $this->err('优惠券编码错误！');
        }
        if (input('status') == 0) {
            if ($coupon['status'] == 0) {
                $this->err('该优惠券已被禁用！');
            }
            $data = [
                'id' => $where['id'],
                'status' => 0
            ];
            $res = model('coupon')->editData($data, $data['id']);
        }

        if (input('status') == 1) {

            if ($coupon['status'] == 1) {
                $this->err('该优惠券已被启用！');
            }
            $data = [
                'id' => $where['id'],
                'status' => 1
            ];
            $res = model('coupon')->editData($data, $data['id']);
        }

        $res ? $this->suc($res) : $this->err('操作失败！');

    }

}